/* Copyright 2017, 2018, Gabriel Czernikier
 *
 * This file is part of Notehaus.
 * Notehaus is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * Notehaus is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with Notehaus.  If not, see <http://www.gnu.org/licenses/>.
 */
/*
 * With regard on memory deallocation, it's arguable how this program neglect to free most of its dynamically allocated memmory.
 * Although the habit of disposing memory is good and healthy, non-conformance with that is acceptable.
 * Considering the "batch" nature of this program, let it be authoritative for the sake of validating these lines, what's said in the manual for Bison 2.4.1, section 3.7.7 (as well as for Bison 3.0.x, section 3.7.6),
 * Freeing Discarded Symbols (node Destructor Decl of info doc): "When [...] convey heap based information, this memory is lost. While this behavior can be tolerable for batch...".
 * Wherever it is discovered a program termination from which an invariance can be acknowledged that memory is left retained, and so a job on disposal incompletely done as for leaving uncleaned "leftovers", it is marked by a message that includes the words "clutter" or "cluttering" as part of it. Neither that it harms nor that the intention is a fixing, however.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "composer.h"
#include "parse_file.h"
#include "types.h"

// Imports functions
void build_map_list();  // map_list.c
void pack_map_list();  // map_list.c
void build_header();  // header.c
void pack_header();  // header.c

int main(int argc, char **argv) {
  if(argc<2) {
    fprintf(stderr, "Program usage: %s <input file>", argv[0]);
    exit(EXIT_FAILURE);
  }
  for(int i=1; i<argc; i++)
    parse_file(argv[i]);
  /* TODO parse_api_stubs(); the pseudo source code form of api exposed information, like what can be extracted from the android-api-doc directory */ 
  /* TODO resolve_types();  see parse_file.h*/
  build_ostr_id_and_pack_str_dt();
  build_otype_id();
  build_otype_list();
  build_oproto_id();
  build_header();
  build_map_list();
  align_bounds();
  pack_ostr_id_ary();
  pack_otype_id();
  pack_otype_list();
  pack_oproto_id();
  pack_map_list();
  pack_header();
  FILE *fw = fopen("classes.dex", "wb");
  for(int i=1; i!=NH_UP_BOUNDS_SZ; i++) {
    fwrite(buffer[i], 1, buf_len[i], fw);
    // fwrite with '\000' repeated as many times as necessary to fill between sections when padding is in place.
    int padlen = up_bounds_ary[i]-(buf_len[i]+up_bounds_ary[i-1]);
    for(int j=0; j<padlen; j++) {
      fwrite("\000", 1, 1, fw);
    }
  }
  fclose(fw);
  exit(EXIT_SUCCESS);
  return 0;
}
